package 华为OD机试真题2023.进阶题;

import utils.MyUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class HJ18_快递投放问题 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(new File("src/main/java/华为OD机试真题2023/进阶题/input/18.txt"));
        MyUtil.START();
        Solution(in);
//        Solution2(in);
        MyUtil.END();
    }

    static void Solution(Scanner in) {
        int N = in.nextInt();
        int M = in.nextInt();
        in.nextLine(); //吸收回车
        Map<String, String> pkg = new HashMap<>();
        for (int i = 0; i < N; i++) {
            String[] sArray = in.nextLine().split(" ");
            pkg.put(sArray[0], sArray[1] + sArray[2]);
        }
        Map<String, List<String>> forbid = new HashMap<>();
        for (int i = 0; i < M; i++) {
            String[] sArray = in.nextLine().split(" ");
            List<String> tmpList = new ArrayList<>();
            for (int j = 2; j < sArray.length; j++) {
                tmpList.add(sArray[j]);
            }
            forbid.put(sArray[0] + sArray[1], tmpList);
        }
        StringBuilder res = new StringBuilder();
        boolean flag = false;
        for (Map.Entry<String, String> entry : pkg.entrySet()) {
            if (forbid.containsKey(entry.getValue())) {
                List<String> fbd = forbid.get(entry.getValue());
                if (fbd.contains(entry.getKey())) {
                    res.append(entry.getKey()).append(" ");
                    flag = true;
                }
            }
        }
        if (!flag) {
            System.out.println("none");
        } else {
            System.out.println(res);
        }
    }

    static void Solution2(Scanner sc) {
        int M = sc.nextInt();
        int N = sc.nextInt();
        sc.nextLine();

        /**
         * key：包裹名
         * value：运输路径
         */
        Map<String, String> mapPkg = new HashMap<>();
        for (int i = 0; i < M; i++) {
            String[] strings = sc.nextLine().split(" ");
            mapPkg.put(strings[0], strings[1] + strings[2]);
        }

        /**
         * key：路径名
         * value：被拦截的包裹名
         */
        Map<String, List<String>> mapNo = new HashMap<>();
        for (int i = 0; i < N; i++) {
            String[] strings = sc.nextLine().split(" ");
            List<String> noList = new ArrayList<>();
            for (int j = 2; j < strings.length; j++) {
                noList.add(strings[j]);
            }
            mapNo.put(strings[0] + strings[1], noList);
        }

        List<String> resList = new ArrayList<>();
        for (Map.Entry<String, List<String>> map : mapNo.entrySet()) {
            String key = map.getKey();  //路径名
            List<String> list = map.getValue(); //被拦截的包裹
            for (String s : list) {
                if (key.equals(mapPkg.get(s))) {  //被拦截的包裹的运输路径等于此路径则表示包裹被拦截
                    resList.add(s);
                }
            }
        }

        System.out.println(resList);
    }
}
